package com.hunttown.mes.impl;

import com.hunttown.common.domain.Page;
import com.hunttown.common.domain.Query;
import com.hunttown.mes.common.utils.StringUtils;
import com.hunttown.mes.rpc.api.IotEnterpriseRpcService;
import com.hunttown.mes.rpc.domain.IotEnterpriseDTO;
import com.hunttown.mes.rpc.domain.query.IotEnterpriseDTOQuery;
import com.hunttown.mes.service.IotEnterpriseService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

import static com.hunttown.mes.convert.IotEnterpriseConverts.convert;

/**
 * created by wangjunfu on 2020-10-05 16:34:04
 */
@Service
public class IotEnterpriseRpcServiceImpl implements IotEnterpriseRpcService {

    private final Logger logger = LoggerFactory.getLogger(IotEnterpriseRpcServiceImpl.class);

    private final IotEnterpriseService iotEnterpriseService;

    @Autowired
    public IotEnterpriseRpcServiceImpl(IotEnterpriseService iotEnterpriseService) {
        this.iotEnterpriseService = iotEnterpriseService;
    }

    @Override
    public IotEnterpriseDTO insert(IotEnterpriseDTO iotEnterpriseDTO) {
        try {
            return convert(iotEnterpriseService.insert(convert(iotEnterpriseDTO)));
        } catch (Exception e) {
            logger.error("IotEnterprise create failed.", e);
            throw new RuntimeException("IotEnterprise create failed, Please order your code.", e);
        }
    }

    @Override
    public Boolean updateInfoById(IotEnterpriseDTO iotEnterpriseDTO) {
        try {
            return iotEnterpriseService.updateInfoById(convert(iotEnterpriseDTO));
        } catch (Exception e) {
            logger.error("IotEnterprise update by id failed.", e);
            throw new RuntimeException("IotEnterprise update by id failed, Please order your code.", e);
        }
    }

    @Override
    public Boolean updateInfoByQuery(IotEnterpriseDTOQuery iotEnterpriseDTOQuery) {
        try {
            Query query = Query.page(1, 5);

            if (iotEnterpriseDTOQuery.getW_id() != null && iotEnterpriseDTOQuery.getW_id() > 0) {
                query.put("w_id", iotEnterpriseDTOQuery.getW_id());
            }

            if (iotEnterpriseDTOQuery.getIds() != null && !iotEnterpriseDTOQuery.getIds().equals("")) {
                query.put("ids", iotEnterpriseDTOQuery.getIds());
            }

            if (iotEnterpriseDTOQuery.getStopFlag() != null) {
                query.put("stopFlag", iotEnterpriseDTOQuery.getStopFlag());
            }

            if (iotEnterpriseDTOQuery.getDeleteFlag() != null) {
                query.put("deleteFlag", iotEnterpriseDTOQuery.getDeleteFlag());
            }

            return iotEnterpriseService.updateInfoByQuery(query);
        } catch (Exception e) {
            logger.error("IotEnterprise update by query failed.", e);
            throw new RuntimeException("IotEnterprise update by query failed, Please order your code.", e);
        }
    }

    @Override
    public IotEnterpriseDTO selectById(Integer id) {
        try {
            return convert(iotEnterpriseService.selectById(id));
        } catch (Exception e) {
            logger.error("IotEnterprise select by id failed.", e);
            throw new RuntimeException("IotEnterprise select by id failed, Please order your code.", e);
        }
    }

    @Override
    public IotEnterpriseDTO selectObjByQuery(IotEnterpriseDTOQuery iotEnterpriseDTOQuery) {
        try {
            Query query = Query.page(1, 5);

            return convert(iotEnterpriseService.selectObjByQuery(query));
        } catch (Exception e) {
            logger.error("IotEnterprise select obj by query failed.", e);
            throw new RuntimeException("IotEnterprise select obj by query failed, Please order your code.", e);
        }
    }

    @Override
    public Page<IotEnterpriseDTO> getListForPage(IotEnterpriseDTOQuery iotEnterpriseDTOQuery) {
        Query query = Query.page(iotEnterpriseDTOQuery.getPn(), iotEnterpriseDTOQuery.getPageSize());

        if (iotEnterpriseDTOQuery.getId() != null && iotEnterpriseDTOQuery.getId() > 0) {
            query.put("id", iotEnterpriseDTOQuery.getId());
        }

        if (iotEnterpriseDTOQuery.getIds() != null && !iotEnterpriseDTOQuery.getIds().equals("")) {
            query.put("ids", iotEnterpriseDTOQuery.getIds());
        }

        if (StringUtils.isNotBlank(iotEnterpriseDTOQuery.getLikeName())) {
            query.put("likeName", iotEnterpriseDTOQuery.getLikeName());
        }

        if (StringUtils.isNotBlank(iotEnterpriseDTOQuery.getShowDate())) {
            query.put("showDate", iotEnterpriseDTOQuery.getShowDate());
        }

        if (StringUtils.isNotBlank(iotEnterpriseDTOQuery.getStartDate())) {
            query.put("startDate", iotEnterpriseDTOQuery.getStartDate());
        }

        if (StringUtils.isNotBlank(iotEnterpriseDTOQuery.getEndDate())) {
            query.put("endDate", iotEnterpriseDTOQuery.getEndDate());
        }

        if (StringUtils.isNotBlank(iotEnterpriseDTOQuery.getOrderBy())) {
            query.put("orderBy", iotEnterpriseDTOQuery.getOrderBy());
        }

        if (iotEnterpriseDTOQuery.getStopFlag() != null) {
            query.put("stopFlag", iotEnterpriseDTOQuery.getStopFlag());
        }

        if (iotEnterpriseDTOQuery.getDeleteFlag() != null) {
            query.put("deleteFlag", iotEnterpriseDTOQuery.getDeleteFlag());
        }

        List<IotEnterpriseDTO> list = convert(iotEnterpriseService.selectByQuery(query));
        Page<IotEnterpriseDTO> page = Page.from(iotEnterpriseDTOQuery.getPn(), iotEnterpriseDTOQuery.getPageSize(), list);
        page.setTotalItems(iotEnterpriseService.countByQuery(query));

        return page;
    }

    @Override
    public Boolean deleteById(Integer id) {
        try {
            return iotEnterpriseService.deleteById(id);
        } catch (Exception e) {
            logger.error("IotEnterprise delete failed.", e);
            throw new RuntimeException("IotEnterprise delete failed, Please order your code.", e);
        }
    }
}